package webservice.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import com.zk.bcutil.DBUtil;
import com.zk.bcutil.Request;
import com.zk.bcutil.Response;
import com.zk.entity.Nc;
import com.zk.entity.Oa;
import com.zk.entity.Org;
import com.zk.entity.Organization;
import com.zk.entity.User;
import com.zk.service.OrgService;
import com.zk.service.OrganizationService;
import com.zk.service.UserService;
import com.zk.test.ESBTest;
import com.zk.util.Util;
import com.zk.util.WebServiceConstants;

import webservice.BCWebservice;

public class BCWebserviceImpl implements BCWebservice {
	
	@Autowired
	private OrganizationService organizationService;
	
	@Autowired
	private UserService userService;
	
	Connection conn = null;
	Statement stat = null;
	ResultSet rs = null;

	@Override
	public String BC_HR_ADDORGANIZATION(String request) {
		// TODO Auto-generated method stub
		Request r = new Request(request);
		Organization org = new Organization();
		
		DBUtil s = new DBUtil();
        ResultSet rs;
		rs = s.Query("select MARK from checkinterface where CKID = 001" );
		
		try {
			while(rs.next()){
				String mark = rs.getString("MARK");
				if("1".equals(mark)){
					return new Response(r.getHeader(), "该接口权限未开放，请联系管理员").getXmlStr();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			s.close();
		} 
		
		SimpleDateFormat sdf =   new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = null;
		try {
			date = sdf.parse(r.getHeader().getSendTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		org = (Organization) r.bindRequestData(org);
		org.setAddtime(date);
		org.setUpdatetime(date);
		
		Organization corg = new Organization();
		corg = organizationService.getOrganizationById(org.getOrgid());
		if(null!=corg){
			return new Response(r.getHeader(), "该机构已经存在").getXmlStr();
		}
		 

		organizationService.addOrganization(org);

		Object obj = null;
		 
		return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, obj).getXmlStr();
	}

	@Override
	public String BC_HR_UPDATEORGANIZATION(String request) {
		
		Request r = new Request(request);
		Organization org = new Organization();
	
		DBUtil s = new DBUtil();
        ResultSet rs;
		rs = s.Query("select MARK from checkinterface where CKID = 002" );
		
		try {
			while(rs.next()){
				String mark = rs.getString("MARK");
				if("1".equals(mark)){
					return new Response(r.getHeader(), "该接口权限未开放，请联系管理员").getXmlStr();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			s.close();
		} 
		org = (Organization) r.bindRequestData(org);
		org.setUpdatetime(new Date());

		organizationService.updateOrganization(org);

		Object obj = null;
		 
		return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, obj).getXmlStr();
	}

	@Override
	public String BC_HR_ORGDELETE(String request) {
		// TODO Auto-generated method stub
		Request r = new Request(request);
		Organization org = new Organization();
	
		DBUtil s = new DBUtil();
        ResultSet rs;
		rs = s.Query("select MARK from checkinterface where CKID = 003" );
		
		try {
			while(rs.next()){
				String mark = rs.getString("MARK");
				if("1".equals(mark)){
					return new Response(r.getHeader(), "该接口权限未开放，请联系管理员").getXmlStr();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			s.close();
		} 
		
		org = (Organization) r.bindRequestData(org);
		org.setUpdatetime(new Date());
		org.setOrgmark("1");
		organizationService.deleteOrganization(org);
		
		Object obj = null;
		 
		return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, obj).getXmlStr();
	}


	@Override
	public String BC_HR_USERADD(String request) {
		Request r = new Request(request);
		User user = new User();
		DBUtil s = new DBUtil();
        ResultSet rs;
		rs = s.Query("select MARK from checkinterface where CKID = 004" );
		
		try {
			while(rs.next()){
				String mark = rs.getString("MARK");
				if("1".equals(mark)){
					return new Response(r.getHeader(), "该接口权限未开放，请联系管理员").getXmlStr();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			s.close();
		} 
		SimpleDateFormat sdf =   new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = null;
		try {
			date = sdf.parse(r.getHeader().getSendTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		user = (User) r.bindRequestData(user);
		user.setAddtime(date);
		user.setUpdatetime(date);

		User cuser = new User();
		cuser = userService.getUserById(user.getUserid());
		if(null!=cuser){
			return new Response(r.getHeader(), "该用户已经存在").getXmlStr();
		}
		userService.addUser(user);

		Object obj = null;
		 
		return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, obj).getXmlStr();
	}

	@Override
	public String BC_HR_USERUPDATE(String request) {
		Request r = new Request(request);
		User user = new User();
	
		DBUtil s = new DBUtil();
        ResultSet rs;
		rs = s.Query("select MARK from checkinterface where CKID = 005" );
		
		try {
			while(rs.next()){
				String mark = rs.getString("MARK");
				if("1".equals(mark)){
					return new Response(r.getHeader(), "该接口权限未开放，请联系管理员").getXmlStr();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			s.close();
		} 
		user = (User) r.bindRequestData(user);
		user.setUpdatetime(new Date());

		userService.updateUser(user);

		Object obj = null;
		 
		return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, obj).getXmlStr();
	}

	@Override
	public String BC_HR_USERDELETE(String request) {
		
		Request r = new Request(request);
		User user = new User();
	
		DBUtil s = new DBUtil();
        ResultSet rs;
		rs = s.Query("select MARK from checkinterface where CKID = 006" );
		
		try {
			while(rs.next()){
				String mark = rs.getString("MARK");
				if("1".equals(mark)){
					return new Response(r.getHeader(), "该接口权限未开放，请联系管理员").getXmlStr();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			s.close();
		} 
		user = (User) r.bindRequestData(user);
		user.setUpdatetime(new Date());
		user.setUsermark("1");
		userService.deleteUser(user);
		
		Object obj = null;
		 
		return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, obj).getXmlStr();
		 
	}

	@Override
	public String BC_OA_GETORGINFO(String request) {
		// TODO Auto-generated method stub
		Request r = new Request(request);
		Oa oa = new Oa();
		DBUtil s = new DBUtil();
        ResultSet rs;
		rs = s.Query("select MARK from checkinterface where CKID = 007" );
		
		try {
			while(rs.next()){
				String mark = rs.getString("MARK");
				if("1".equals(mark)){
					return new Response(r.getHeader(), "该接口权限未开放，请联系管理员").getXmlStr();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			s.close();
		} 
		oa = (Oa) r.bindRequestData(oa);
		int timeNum = Integer.valueOf(oa.getTime());
		List<String> list=new ArrayList<String>();
		if(null != oa.getDeptid()){
			String[] dps = oa.getDeptid().split(",");
			list = java.util.Arrays.asList(dps);
		}

		System.out.println(list.size());
		Date currDate = new Date();
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String endtime = sdf.format(currDate);
	    Date afterDate = new Date(currDate.getTime() - timeNum*60*1000);
	    String starttime = sdf.format(afterDate);
	    
	    //遍历json转换表
	    String JsonContext = new Util().ReadFile(BCWebserviceImpl.class.getClassLoader().getResource("tran-oa.json").getPath());  
		JSONArray json = JSONArray.fromObject(JsonContext).getJSONArray(0);
	    List<Organization> orgList = organizationService.getOrganization(starttime, endtime, list);
	    if(null != orgList && !orgList.isEmpty()){
	    	for (Organization organization : orgList) {
	    		if(null != organization.getUnitfullname() && !"".equals(organization.getUnitfullname())){
	    			String temp = organization.getUnitfullname();
	    			if(json.size()>0){
	    				for(int i = 0; i < json.size(); i++){
	    					JSONObject job = json.getJSONObject(i);
	    					JSONArray hr = JSONArray.fromObject(job.get("hr"));
	    					for (Object hrname : hr) {
	    						if(temp.equals(hrname)){
	    							organization.setUnitfullname(job.get("name").toString());
	    						}
	    					}
	    				}
	    				 
	    			}
	    		} 
			}
	    	return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, orgList).getXmlStr();
	    }
	    return new Response(r.getHeader(), "无更新内容！").getXmlStr();
		
	}

	@Override
	public String BC_OA_GETUSERINFO(String request) {
		Request r = new Request(request);
		Oa oa = new Oa();
		DBUtil s = new DBUtil();
        ResultSet rs;
		rs = s.Query("select MARK from checkinterface where CKID = 008" );
		
		try {
			while(rs.next()){
				String mark = rs.getString("MARK");
				if("1".equals(mark)){
					return new Response(r.getHeader(), "该接口权限未开放，请联系管理员").getXmlStr();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			s.close();
		} 
		oa = (Oa) r.bindRequestData(oa);
		
		
		int timeNum = Integer.valueOf(oa.getTime());
		List<String> list=new ArrayList<String>();
		if(null != oa.getDeptid()){
			String[] dps = oa.getDeptid().split(",");
			list = java.util.Arrays.asList(dps);
		}

		Date currDate = new Date();
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String endtime = sdf.format(currDate);

	    Date afterDate = new Date(currDate.getTime() - timeNum*60*1000);
	    String starttime = sdf.format(afterDate);

	    //List<User> userlist = userService.getUser(starttime, endtime, list);
	    String JsonContext = new Util().ReadFile(BCWebserviceImpl.class.getClassLoader().getResource("tran-oa.json").getPath());  
		JSONArray json = JSONArray.fromObject(JsonContext).getJSONArray(1);
	    List<User> userlist = userService.getUser(starttime, endtime, list);
	    if(null != userlist && !userlist.isEmpty()){
	    	for (User user : userlist) {
	    		if(null != user.getCompany() && !"".equals(user.getCompany())){
	    			String temp = user.getCompany();
	    			if(json.size()>0){
	    				for(int i = 0; i < json.size(); i++){
	    					JSONObject job = json.getJSONObject(i);
	    					JSONArray hr = JSONArray.fromObject(job.get("hr"));
	    					for (Object hrname : hr) {
	    						if(temp.equals(hrname)){
	    							user.setCompany(job.get("name").toString());
	    						}
	    					}
	    				}
	    				 
	    			}
	    		} 
			}
	    	return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, userlist).getXmlStr();
	    }
	     
	    /*if(null != userlist && !userlist.isEmpty()){
	    	return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, userlist).getXmlStr();
	    }*/
	    //List<Person> p = userService.getPerson(starttime, endtime, list);
	    
	    return new Response(r.getHeader(), "无更新内容！").getXmlStr();
	}

	@Override
	public String BC_NC_GETUSERINFO(String request) {
		Request r = new Request(request);
		Nc oa = new Nc();
		DBUtil s = new DBUtil();
        ResultSet rs;
		rs = s.Query("select MARK from checkinterface where CKID = 009" );
		
		try {
			while(rs.next()){
				String mark = rs.getString("MARK");
				if("1".equals(mark)){
					return new Response(r.getHeader(), "该接口权限未开放，请联系管理员").getXmlStr();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			s.close();
		} 
		oa = (Nc) r.bindRequestData(oa);
		
		
		int timeNum = Integer.valueOf(oa.getTime());
		List<String> list=new ArrayList<String>();
		if(null != oa.getDeptid()){
			String[] dps = oa.getDeptid().split(",");
			list = java.util.Arrays.asList(dps);
		}

		Date currDate = new Date();
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String endtime = sdf.format(currDate);

	    Date afterDate = new Date(currDate.getTime() - timeNum*60*1000);
	    String starttime = sdf.format(afterDate);

	    //List<User> userlist = userService.getUser(starttime, endtime, list);
	    String JsonContext = new Util().ReadFile(BCWebserviceImpl.class.getClassLoader().getResource("tran-oa.json").getPath());  
		JSONArray json = JSONArray.fromObject(JsonContext).getJSONArray(1);
	    List<User> userlist = userService.getUser(starttime, endtime, list);
	    List<User> ul = new ArrayList<User>();
	    if(null != userlist && !userlist.isEmpty()){
	    	for (User user : userlist) {
	    		User u = new User();

	    		if(null != user.getCompany() && !"".equals(user.getCompany())){
	    			String temp = user.getCompany();
	    			if(json.size()>0){
	    				for(int i = 0; i < json.size(); i++){
	    					JSONObject job = json.getJSONObject(i);
	    					JSONArray hr = JSONArray.fromObject(job.get("hr"));
	    					for (Object hrname : hr) {
	    						if(temp.equals(hrname)){
	    							user.setCompany(job.get("name").toString());
	    						}
	    					}
	    				}
	    				 
	    			}
	    		} 
			}
	    	return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, userlist).getXmlStr();
	    }
	     
	    /*if(null != userlist && !userlist.isEmpty()){
	    	return new Response(r.getHeader(), WebServiceConstants.MSG_RESULT_SUCCESS_DESC, userlist).getXmlStr();
	    }*/
	    //List<Person> p = userService.getPerson(starttime, endtime, list);
	    
	    return new Response(r.getHeader(), "无更新内容！").getXmlStr();
	}

}
